// 放置轮播图数据的模块, 涉及到多个页面使用, 所有将这个数据使用pinia维护
import { defineStore } from 'pinia'
import request from '@/utils/request'
import { ApiRes, BannerItem, NewGoodsItem, HotGoodsItem, Brand, HomeProduct, Special } from '@/types/data'
const useHomeStore = defineStore('home', {
  state() {
    return {
      slideList: [] as BannerItem[], //轮播图的数据
      newGoodsList: [] as NewGoodsItem[], // 新鲜好物的数据
      hotGoodsList: [] as HotGoodsItem[], // 人气推荐的数据
      brandList: [] as Brand[], // 热门品牌的数据
      productList: [] as HomeProduct[], // 商品模块的数据
      specialList: [] as Special[]// 最新专题的数据
    }
  },
  actions: {
    // 发送请求获取轮播图的数据
    async getSlideList() {
      const res = await request.get<ApiRes<BannerItem[]>>('/home/banner')
      this.slideList = res.data.result
    },
    // 发送请求获取新鲜好物的数据
    async getNewGoodsList() {
      const res = await request.get<ApiRes<NewGoodsItem[]>>('/home/new')
      this.newGoodsList = res.data.result
    },
    // 发送请求获取人气推荐的数据
    async getHotGoodsList() {
      const res = await request.get<ApiRes<HotGoodsItem[]>>('/home/hot')
      this.hotGoodsList = res.data.result
    },
    // 发送请求获取热门品牌的数据
    async getBrandList() {
      const res = await request.get<ApiRes<Brand[]>>('/home/brand')
      this.brandList = [...res.data.result, ...res.data.result]
    },
    // 发送请求获取商品模块的数据
    async getProductList() {
      const res = await request.get<ApiRes<HomeProduct[]>>('/home/goods')
      this.productList = res.data.result
    },
    // 发送请求获取最新专题的数据
    async getSpecialList() {
      const res = await request.get<ApiRes<Special[]>>('/home/special')
      this.specialList = res.data.result
    },
  },
  getters: {}
})

export default useHomeStore
